package com.hst.capacity.domain.service.impl;

import com.hst.capacity.domain.common.constants.Constants;
import com.hst.capacity.domain.model.entity.member.MemberEntity;
import com.hst.capacity.domain.service.MemberService;
import com.hst.capacity.infra.persistence.jpa.member.MemberRepository;
import com.hst.capacity.infra.util.SecurityUtil;
import com.hst.capacity.infra.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.security.NoSuchAlgorithmException;

@Service
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberRepository memberRepository;

    @Override
    public MemberEntity findByMemberAccount(String mobile) {
        return memberRepository.findByMemberAccount(mobile);
    }

    /**
     * 创建乘客信息
     * @param mobile
     * @return
     */
    @Override
    public MemberEntity createNewMember(String mobile) {
        MemberEntity member = memberRepository.findByMemberAccount(mobile);
        if (member == null) {
            //2，乘客账户不存在时，创建乘客账户
            member = new MemberEntity();
            member.setMemberAccount(mobile);
            String pwd = StringUtil.getRndPassword(6);
            try {
                member.setPassword(SecurityUtil.SecurityDigest(pwd));
            } catch (NoSuchAlgorithmException e) {
                member.setPassword("");
            }
            member.setMemberType(Constants.MemberType.PERSON);
            member.setStatus(Constants.MemberStatus.USABLE);
            member.setRegisterFrom("capacity");
            member = (MemberEntity) memberRepository.save(member);



        }
        return member;
    }

}
